/**
 * 根据服务端返回的数据创建路由
 */
import { getConfig } from './config'
// 布局处理
import { aboutLayout, RouterViewLayout } from '../components/layout/index.js'

/**
 * 创建一个动态的路由对象
 * @param {*} item
 */
function createRouteObjects(item, menuType) {
  let { menuId, icon, name, path, title, hidden, component, data, code } = getConfig(item)
  if (name) {
    var route = {
      path,
      hidden,
      component,
      name,
      meta: {
        menuType,
        menuId,
        title,
        icon,
        isDynamic: true,
        isTab: true,
        data,
        code
      }
    }
    //手动映射供应商相关meta数据&映射招标相关meta数据
    switch (route.path) {
      case '/projectCase':
        route.meta.fatherType = '0'
        route.meta.type = '0'
        break
      case '/projectqualificationHonor':
        route.meta.fatherType = '0'
        route.meta.type = '1'
        break
      case '/projectexpertTeam':
        route.meta.fatherType = '0'
        route.meta.type = '2'
        break
      case '/financeAndTaxCase':
        route.meta.fatherType = '1'
        route.meta.type = '0'
        break
      case '/financeAndTaxqualificationHonor':
        route.meta.fatherType = '1'
        route.meta.type = '1'
        break
      case '/financeAndTaxexpertTeam':
        route.meta.fatherType = '1'
        route.meta.type = '2'
        break
        case '/financeAndTaxCase':
        route.meta.fatherType = '1'
        route.meta.type = '0'
        break
      case '/lawCase':
        route.meta.fatherType = '2'
        route.meta.type = '0'
        break
      case '/lawqualificationHonor':
        route.meta.fatherType = '2'
        route.meta.type = '1'
        break
      case '/lawexpertTeam':
        route.meta.fatherType = '2'
        route.meta.type = '2'
        break
        // 分割线///////////////////////
      case '/invitationAll':
        route.meta.fatherType = 0
        route.meta.type = 5
        break
      case '/invitationcg':
        route.meta.fatherType = 0
        route.meta.type = 0
        break
      case '/invitationzb':
        route.meta.fatherType = 0
        route.meta.type = 1
        break
      case '/invitationbd':
        route.meta.fatherType = 0
        route.meta.type = 2
        break
      case '/invitationfb':
        route.meta.fatherType = 0
        route.meta.type = 3
        break
      case '/invitationly':
        route.meta.fatherType = 0
        route.meta.type = 4
        break
        //////////////////
      case '/invitationlaw':
        route.meta.fatherType = 1
        route.meta.type = 5
        break
      case '/purchaselaw':
        route.meta.fatherType = 1
        route.meta.type = 0
        break
      case '/purchasezb':
        route.meta.fatherType = 1
        route.meta.type = 1
        break
      case '/purchaseby':
        route.meta.fatherType = 1
        route.meta.type = 2
        break
      case '/purchasefb':
        route.meta.fatherType = 1
        route.meta.type = 3
        break
      case '/purchasely':
        route.meta.fatherType = 1
        route.meta.type = 4
        break
      ///////
      case '/financeTax':
        route.meta.fatherType = 2
        route.meta.type = 5
        break
      case '/financeTaxzb':
        route.meta.fatherType = 2
        route.meta.type = 0
        break
      case '/financeTaxcj':
        route.meta.fatherType = 2
        route.meta.type = 1
        break
      case '/financeTaxby':
        route.meta.fatherType = 2
        route.meta.type = 2
        break
      case '/financeTaxfb':
        route.meta.fatherType = 2
        route.meta.type = 3
        break
      case '/financeTaxly':
        route.meta.fatherType = 2
        route.meta.type = 4
        break
      default:
        break
    }
    
    return route
  }
}
// 2 为其添加一个左右布局的 component 和一个 children
/**
 * 添加动态(菜单)路由
 * @param {*} menuList 菜单列表
 * @param {*} routes 递归创建的动态(菜单)路由
 */
export function fnAddDynamicMenuRoutes(menuList = [], routes = [], level = 1) {
  for (let item of menuList) {
    let { menuId, icon, name, path, title, children, hidden } = getConfig(item)
    // 如果是类型为1为全屏布局
    if (item.menuType == 1 || item.menuType == 3) {
      let result = createRouteObjects(item, item.menuType)
      result ? routes.push(result) : ''
    } else if (item.menuType == 2) {
      //模板为左右布局,应当有children子集
      let route = {
        path,
        hidden,
        // 第二层开始添加不含侧边栏的路由插槽，否则出现多个侧边栏的情况
        component: level > 1 ? RouterViewLayout : aboutLayout, //左右布局模板
        name,
        meta: {
          menuType: item.menuType,
          menuId,
          title,
          icon,
          isDynamic: true,
          isTab: false
        },
        children: []
      }
      //子集添加
      fnAddDynamicMenuRoutes(children, route.children, level + 1)
      routes.push(route)
    } else if (item.menuType == 99 && children && children.length >= 1) {
      //如果只是左侧菜单
      let temp = children
      fnAddDynamicMenuRoutes(temp, routes)
    }
  }
  return routes
}
